package com.fingard.dsp.bank.directConfig;

import com.fingard.dsp.DSPSet;
import com.fingard.dsp.bankset.BankFront;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

public class ListenerSet {
    /*
            <listenerSet enable="true" readme="侦听服务，用于接收通知">
                <port readme="侦听端口">706</port>
                <protocol readme="通讯协议">http</protocol>
                <prefix readme="服务地址前缀">/Notify</prefix>
                <invoking readme="回调方法">acceptNotify</invoking>
            </listenerSet>
     * */
    public boolean enable = false;
    public int listenerPort;
    public String protocol;
    public String prefix;
    public String invoking;
    public String charset;
    public BankFront bankFront;
    /**超时时间：秒*/
    public int timeoutSec;
    
    public boolean keepAlive = false;
    
    /*full：全双工Full Duplex，half：半双工Half Duplex*/
    public String duplexing;
    
    public int initHeadByteCount = -1;

    /** https使用 */
    public String keyStore;//密钥库
    public String keyStorePwd;//密钥库访问密码
    public String encryProtocol;//加密协议SSL、TLS

    public void fromXElement(XElement p_XE) throws NumberFormatException, Exception {
        enable = p_XE.getAttrValueAsBool("enable");
        if (p_XE.getSingleTextNotNull("port").length() > 0) {
            listenerPort = Integer.parseInt(p_XE.getSingleTextNotNull("port"));
            protocol = p_XE.getSingleTextNotNull("protocol");
            prefix = p_XE.getSingleTextNotNull("prefix");
            if (StringHelper.isNullOrEmpty(prefix)) {
                prefix = "/";
            }
            invoking = p_XE.getSingleTextNotNull("invoking");
            charset = p_XE.getSingleTextNotNull("charset");
            if (StringHelper.isNullOrEmpty(charset)) {
                charset = java.nio.charset.Charset.defaultCharset().name();//charset = "gbk";
            }
            String ttSec = p_XE.getSingleTextNotNull("timeoutSec");
            if(StringHelper.hasAnyChar(ttSec)){
            	timeoutSec = Integer.parseInt(ttSec);
            }else{
            	timeoutSec = 10;//默认10秒
            }
            if("true".equals(p_XE.getSingleTextNotNull("keepAlive").toLowerCase())){
            	keepAlive = true;
            }
            duplexing = p_XE.getSingleTextNotNull("duplexing").toLowerCase();
            String initHeadByte = p_XE.getSingleTextNotNull("initHeadByteCount");
            if(StringHelper.hasAnyChar(initHeadByte)){
            	initHeadByteCount = Integer.parseInt(initHeadByte);
            }
            keyStore = DSPSet.replaceFilePath(p_XE.getSingleTextNotNull("keyStore"));
            keyStorePwd = p_XE.getSingleTextNotNull("keyStorePwd");
            encryProtocol=p_XE.getSingleTextNotNull("encryProtocol");
        }
    }
}
